// Render Window
// (c) jimon game studio

#ifndef JEH_JERENDERWINDOW
#define JEH_JERENDERWINDOW

#include "IDevice.h"
#include "IRender.h"

namespace je
{
	namespace video
	{
		//! Render Window
		class jeRenderWindow:public je::jeUnknown
		{
		private:
			device::IDevice * Device;
			IRender * Render;

			device::IWindow * Window;
			IRenderContext * RenderContext;
		public:
			//! Constructor
			jeRenderWindow(device::IDevice * device,IRender * render,const jeVector2di & Size,const jeStringd & Title,u1 VSync = true,u1 FullScreen = false,s32 Depth = 0,s32 Hertz = 0,s32 MSAA = 0,const void * PlatformIconId = NULL)
				:Device(device),Render(render)
			{
				Device->Grab();
				Render->Grab();

				Window = Device->CreateWindow(Size,Title,FullScreen,Depth,Hertz,MSAA,PlatformIconId);
				Window->Grab();

				RenderContext = Render->CreateContext();

				RenderContext->SetWindow(Window);
				RenderContext->SetFullWindowUse(true);

				Render->SetContext(RenderContext);

				RenderContext->SetVSyncFlag(VSync);
			}

			//! Destructor
			~jeRenderWindow()
			{
				if(Render->GetContext() == RenderContext)
					Render->SetContext(NULL);

				RenderContext->SetWindow(NULL);

				RenderContext->Drop();
				RenderContext = NULL;

				Window->Drop();
				Device->DeleteWindow(Window);

				Window = NULL;

				Render->Drop();
				Render = NULL;

				Device->Drop();
				Device = NULL;
			}

			//! Setup Context
			void SetupContext()
			{
				if(Render->GetContext() == RenderContext)
					Render->SetContext(RenderContext);
			}

			//! Get Device
			device::IDevice * GetDevice()
			{
				return Device;
			}

			//! Return Window
			device::IWindow * GetWindow()
			{
				return Window;
			}

			//! Return Render Context
			IRenderContext * GetRenderContext()
			{
				return RenderContext;
			}

			//! Return Render
			IRender * GetRender()
			{
				return Render;
			}

			//! Create New Context
			IRenderContext * CreateContext()
			{
				IRenderContext * TempRenderContext = Render->CreateContext();

				RenderContext->SetWindow(Window);
				RenderContext->SetFullWindowUse(Render->GetContext()->IsFullWindowUse());
				RenderContext->SetVSyncFlag(Render->GetContext()->GetVSyncFlag());

				return TempRenderContext;
			}
		};
	}
}

#endif
